<?php

namespace app\api\controller;

use think\Controller;

class Message extends Controller
{
    /**
     * 未读消息数量
     */
    public function message_count($header, $data, $user)
    {
        if($header['uid']<=0){
            return array('code' => 1, 'msg' => '未读消息数量', 'data' => array('num'=>0));
        }
        $message=db("message a")
//            ->field("count(a.id) as all_count")
            ->where(array("a.status"=>1,"a.user_id"=>array('in',"0,".$header['uid'])))
//            ->group("a.status")
            ->count();
        $read=db('message_log b')
            ->join("fr_message a","b.m_id=a.id","left")
            ->where(array("a.status"=>1,"b.u_id"=>$header['uid']))
            ->count();
        $num=$message-$read;
        return array('code' => 1, 'msg' => '未读消息数量', 'data' => array('num'=>$num));
    }
    /**
     * 消息列表
     */
    public function message_list($header, $data, $user)
    {
        $check = check_array($data, array('page','size'));
        if ($check['code'] != 1) {
            return $check;
        }
        $page=$data['page'];
        $size=$data['size'];
        $now=($page-1)*$size;
        if($header['uid']>0){
            $uid=$header['uid'];
        }else{
            $uid=0;
        }
        $message=db("message a")
            ->field("a.id,a.title,a.create_time,(select b.id from fr_message_log b where b.m_id=a.id and b.u_id=".$uid.") as read_id")
            ->where(array("a.status"=>1,"a.user_id"=>array('in',"0,".$header['uid'])))
            ->order("a.id desc")
            ->limit("$now,$size")
            ->select();
        $message1=array();
        foreach ($message as $v){
            if($v['read_id']>0 || $uid<=0){
                $is_read=1;
            }else{
                $is_read=0;
            }
            $message1[]=array('id'=>$v['id'],'title'=>$v['title'],'create_time'=>date('Y-m-d',$v['create_time']),'is_read'=>$is_read);
        }
        $count=db("message a")
            ->where(array("a.status"=>1,"a.user_id"=>array('in',"0,".$header['uid'])))
            ->order("a.id desc")
            ->count();
        $max_page=ceil($count/$size);
        return array('code' => 1, 'msg' => '消息列表', 'data' => array('data'=>$message1,'max_page'=>$max_page));
    }
    /**
     * 消息详情
     */
    public function message_details($header, $data, $user)
    {
        $check = check_array($data, array('id'));
        if ($check['code'] != 1) {
            return $check;
        }
        $id=$data['id'];
        if($header['uid']>0){
            $uid=$header['uid'];
        }else{
            $uid=0;
        }
        $message=db("message a")
            ->where(array("a.status"=>1,"a.user_id"=>array('in',"0,".$header['uid']),'a.id'=>$id))
            ->find();
        if(empty($message)){
            return array('code' => 2, 'msg' => '没有该消息', 'data' => array());
        }
        if($uid>0){
            $log=db("message_log")
                ->where(array('m_id'=>$message['id'],'u_id'=>$uid))
                ->find();
            if(empty($log)){
                db("message_log")
                    ->insertGetId(array('u_id'=>$uid,'m_id'=>$message['id'],'create_time'=>time()));
            }
        }
        $message1=array('id'=>$message['id'],'title'=>$message['title'],'create_time'=>date('Y-m-d H:i:s',$message['create_time']),'content'=>$message['content']);
        return array('code' => 1, 'msg' => '消息详情', 'data' => array('message'=>$message1));
    }
    /**
     * 标为已读
     */
    public function message_read($header, $data, $user)
    {
        $check = check_array($data, array('ids'));
        if ($check['code'] != 1) {
            return $check;
        }
        $ids=$data['ids'];
        if($header['uid']>0){
            $uid=$header['uid'];
        }else{
            return array('code' => 2, 'msg' => '未登录', 'data' => array());
        }
        $message=db("message a")
            ->where(array("a.status"=>1,"a.user_id"=>array('in',"0,".$header['uid']),'a.id'=>array('in',$ids)))
            ->group("a.id")
            ->select();
        if(empty($message)){
            return array('code' => 2, 'msg' => '没有该消息', 'data' => array());
        }
        foreach ($message as $v){
            $log=db("message_log")
                ->where(array('m_id'=>$v['id'],'u_id'=>$uid))
                ->find();
            if(empty($log)){
                db("message_log")
                    ->insertGetId(array('u_id'=>$uid,'m_id'=>$v['id'],'create_time'=>time()));
            }
        }
        return array('code' => 1, 'msg' => '已标为已读', 'data' => array());
    }
}